package com.example.drive.net.request

import android.os.Build
import okhttp3.Headers
import okhttp3.Request

abstract class OkHttpRequest constructor(url: String, tag: Any) {
    companion object {
        const val BRAND = "brand"
        const val MODEL = "model"
        const val SDK_VERSION = "sdkVersion"
    }

    protected var builder = Request.Builder()

    private var headerBuilder: Headers.Builder = Headers.Builder()

    init {
        builder.url(url).tag(tag)
        // 添加公共请求头
        headerBuilder.add(BRAND, Build.BRAND)
        headerBuilder.add(MODEL, Build.MODEL)
        headerBuilder.add(SDK_VERSION, Build.VERSION.SDK_INT.toString())
        builder.headers(headerBuilder.build())
    }

    /**
     * 获取Request,由子类实现
     */
    abstract fun createRequest(): Request

    fun addHeads(heads: Map<String, String>) {
        for (head in heads) {
            headerBuilder.add(head.key, head.value)
        }
        builder.headers(headerBuilder.build())
    }
}